home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / us20src.zip / FILE.C < prev    next >
C/C++ Source or Header  |  1992-06-26  |  2KB  |  88 lines

  1. /*    FILE:    File handling for MicroSPELL 2.0
  2.         Spell Checker and Corrector
  3.  
  4.         (C)opyright May 1987,1992 by Daniel Lawrence
  5.         All Rights Reserved
  6. */
  7.  
  8. #include    <stdio.h>
  9. #include    "dopt.h"
  10. #include    "dstruct.h"
  11. #include    "ddef.h"
  12. #include    "dpath.h"
  13.  
  14. /*    popen():    open a file for read, looking down the path */
  15.  
  16. FILE *popen(file, mode)
  17.  
  18. char *file;    /* file name to open */
  19. char *mode;    /* mode for file open */
  20.  
  21. {
  22.     char *flook();
  23.  
  24.     return(fopen(flook(file), mode));
  25. }
  26.  
  27. /*    Look up the existance of a file along the normal or PATH
  28.     environment variable. Look first in the HOME directory if
  29.     asked and possible
  30. */
  31.  
  32. char *flook(fname)
  33.  
  34. char *fname;    /* base file name to search for */
  35.  
  36. {
  37.     register char *path;    /* environmental PATH variable */
  38.     register char *sp;    /* pointer into path spec */
  39.     register int i;        /* index */
  40.     register FILE *fp;    /* trial fp */
  41.     static char fspec[NSTRING];    /* full path spec to search */
  42.     char *getenv();
  43.  
  44. #if    ((MSDOS) & (TURBO | LATTICE | AZTEC | MSC)) | V7 | USG | BSD
  45.  
  46.     /* get the PATH variable */
  47.     path = getenv("PATH");
  48.     if (path != NULL)
  49.         while (*path) {
  50.  
  51.             /* build next possible file spec */
  52.             sp = fspec;
  53.             while (*path && (*path != PATHCHR))
  54.                 *sp++ = *path++;
  55.             if (*(sp - 1) != '/' && *(sp - 1) != '\\')
  56.                 *sp++ = '/';
  57.             *sp = 0;
  58.             strcat(fspec, fname);
  59.  
  60.             /* and try it out */
  61.             fp = fopen(fspec, "r");
  62.             if (fp != NULL) {
  63.                 fclose(fp);
  64.                 return(fspec);
  65.             }
  66.  
  67.             if (*path == PATHCHR)
  68.                 ++path;
  69.         }
  70. #endif
  71.  
  72.     /* look it up via the old table method */
  73.     for (i=2; i < NPNAMES; i++) {
  74.         strcpy(fspec, pathname[i]);
  75.         strcat(fspec, fname);
  76.  
  77.         /* and try it out */
  78.         fp = fopen(fspec, "r");
  79.         if (fp != NULL) {
  80.             fclose(fp);
  81.             return(fspec);
  82.         }
  83.     }
  84.  
  85.     /* return the original...hope it is in the current directory */
  86.     return(fname);
  87. }
  88.